package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.ViewsDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.ViewsEntity;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Views
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .Views
 */

public class ViewsDAO extends HibernateDaoSupport implements ViewsDAOInterface {
    private static final Logger log = LoggerFactory.getLogger(ViewsDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(ViewsEntity transientInstance) {
        log.debug("saving Views instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(ViewsEntity persistentInstance) {
        log.debug("deleting Views instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public ViewsEntity findById(Integer id) {
        log.debug("getting Views instance with id: " + id);
        try {
            ViewsEntity instance = (ViewsEntity) getHibernateTemplate().load(ViewsEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<ViewsEntity> findByExample(ViewsEntity instance) {
        log.debug("finding Views instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List<ViewsEntity> findByProperty(String propertyName, Object value) {
        log.debug("finding Views instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from ViewsEntity as model where model."
                    + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<ViewsEntity> findByIcafeProject(Object icafeProject) {
        return findByProperty(ICAFE_PROJECT, icafeProject);
    }

    public List<ViewsEntity> findByModule(Object module) {
        return findByProperty(MODULE, module);
    }

    public List<ViewsEntity> findByVersion(Object version) {
        return findByProperty(VERSION, version);
    }

    public List<ViewsEntity> findByViewName(Object viewName) {
        return findByProperty(VIEW_NAME, viewName);
    }

    public List<ViewsEntity> findByUrl(Object url) {
        return findByProperty(URL, url);
    }

    public List<ViewsEntity> findAll() {
        log.debug("finding all Views instances");
        try {
            String queryString = "from ViewsEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public ViewsEntity merge(ViewsEntity detachedInstance) {
        log.debug("merging Views instance");
        try {
            ViewsEntity result = (ViewsEntity) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(ViewsEntity instance) {
        log.debug("attaching dirty Views instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(ViewsEntity instance) {
        log.debug("attaching clean Views instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static ViewsDAOInterface getFromApplicationContext(ApplicationContext ctx) {
        return (ViewsDAOInterface) ctx.getBean("ViewsDAO");
    }

    public List<Integer> findViewsEntityByModuleAndIcafeProject(String moduleName, String icafeProjectName) {
        log.debug("finding ViewsEntity By Module And IcafeProject");
        try {
            Query query = this.getSession().createQuery(
                    "select v.id from ViewsEntity v where v.icafeProject = :icafeProject and v.module = :module");
            query.setParameter("icafeProject", icafeProjectName.trim());
            query.setParameter("module", moduleName.trim());
            List<Integer> viewids = query.list();
            if (viewids == null) {
                return new ArrayList();
            }
            return viewids;
        } catch (RuntimeException re) {
            log.error("finding ViewsEntity By Module And IcafeProject failed", re);
            throw re;
        }
    }
}